package bb.lanxing.calc.calculator;

import bb.lanxing.calc.data.HeartratePoint;
import bb.lanxing.util.Log;

public class HeartrateCalc {
    final static private String TAG = "HeartrateCalc";
    private static final int VALID_INTERVAL = 3000;
    private HeartratePoint lastHeartratePoint;
    private long lastValidHeartrateTime;

    public void calc(HeartratePoint heartratePoint) {
        if (this.lastHeartratePoint == null) {
            this.lastHeartratePoint = heartratePoint;
            return;
        }
        int heartrate = heartratePoint.getHeartrate();
        int i = 30;
        if (heartrate > 0) {
            this.lastValidHeartrateTime = System.currentTimeMillis();
            if (heartrate > 250) {
                heartrate = 250;
            }
            if (heartrate >= 30) {
                i = heartrate;
            }
        } else {
            i = System.currentTimeMillis() - this.lastValidHeartrateTime < 3000 ? this.lastHeartratePoint.getHeartrate() : 0;
        }
        Log.v(TAG, "HeartrateCalc, heartrate = " + i);
        heartratePoint.setHeartrate(i);
        this.lastHeartratePoint = heartratePoint;
    }

    public void calc01(HeartratePoint heartratePoint) {
        if (this.lastHeartratePoint == null) {
            this.lastHeartratePoint = heartratePoint;
            return;
        }
        int heartrate = heartratePoint.getHeartrate();
        int i = 30;
        if (heartrate > 0) {
            this.lastValidHeartrateTime = System.currentTimeMillis();
            if (heartrate > 250) {
                heartrate = 250;
            }
            if (heartrate >= 30) {
                i = heartrate;
            }
        } else {
            i = heartrate;
        }
        Log.v(TAG, "HeartrateCalc, heartrate = " + i);
        heartratePoint.setHeartrate(i);
        this.lastHeartratePoint = heartratePoint;
    }

    public void release() {
        this.lastHeartratePoint = null;
        this.lastValidHeartrateTime = 0L;
    }
}
